import { TextInput, SelectArrayInput, required } from 'react-admin';
import { Button, SimpleForm,SaveButton   } from 'react-admin';

import { useState } from 'react'
import { useRefresh, useNotify } from 'react-admin';
import { dataProvider } from '../../dataProvider/DataProvider'

export const PushContactRankingToWikiBtn = (props) => {
    const [show, setShow] = useState(false);
    const [disable, setDisable] = useState(false);
    const [contactEvents, setContactEvents] = useState([]);
    const { label } = props;
    const refresh = useRefresh();
    const notify = useNotify();

    const onSubmit = (data) => {
        if (data == null) {
            return;
        }
        const pushParam = {
            wikiUserName: data.wikiUserName,
            wikiPwd: data.wikiPwd
        }
        notify('开始刷新wiki...', { type: 'info' })
        setDisable(true)
        dataProvider.pushContactRanking(pushParam, ret => {
            setDisable(false);
            if (ret.isSucceed) {
                setShow(false);
                refresh();
                notify(ret.msg, { type: 'info' })
            }
        })


    }
    const onClick = () => {
        if (!show) {
            dataProvider.getContactEventDropdown(res => setContactEvents(res));
        }
        setShow(!show)
    }
  
    return (
        <div>
            <Button label={label} onClick={onClick} />
            {show ? (<div>
                        <SimpleForm onSubmit={onSubmit} disabled={disable}
                            toolbar={<SaveButton label ='确认刷新'  />}       >
                            <TextInput label="Wiki 用户名" source="wikiUserName" validate={required()}></TextInput>
                            <TextInput label="Wiki 密码" source="wikiPwd" type="password" validate={required()}></TextInput>
                       
                        </SimpleForm> 
                    </div>) 
                   : null}
        </div>
    )
}